package com.pbdatabase.core.query;

import com.pbdatabase.core.indexer.Indexer;
import com.pbdatabase.core.main.IndexProcessor;
import com.pbdatabase.core.main.ProtoProcessor;
import org.apache.log4j.Logger;

public class ManipulationQueryHandler {
	private static Logger _logger = Logger.getLogger(ManipulationQueryHandler.class);

	public Object select(String protoName, String value) {
		_logger.info("Trying to select protocol buffers for: " + protoName + " : " + value);
		IndexProcessor indexProcessor = IndexProcessor.getInstance();
		Indexer indexer = indexProcessor.getIndexer(protoName);
		Object protoMessage = indexer.getRecord(value);
		return protoMessage;
	}

	public void insert(String protoName, Object protoMessage, String value) { // TODO: Add reflection to obtain value via reflection
		_logger.info("Trying to insert protocol buffer for: " + protoName + " : " + value);
		String messageFile = ProtoProcessor.saveMessage(protoName, protoMessage);
		IndexProcessor indexProcessor = IndexProcessor.getInstance();
		Indexer indexer = indexProcessor.getIndexer(protoName);
		indexer.addRecord(value, messageFile);
	}

	public void delete(String protoName, Object protoMessage) {
		_logger.info("Trying to delete protocol buffer for: " + protoName);
		// TODO: Implement delete
	}

	public void update(String protoName, Object protoMessage) {
		_logger.info("Trying to update protocol buffer for: " + protoName);
		// TODO: Implement update
	}
}
